package com.dajie.pt.service.utils;

import com.dajie.pt.service.beans.MessageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.sql.Connection;
import java.util.*;

/**
 * Created by zhihaosong on 15-11-16.
 */
public class SmsSend implements Runnable {
    private static Logger logger = LoggerFactory.getLogger(SmsSend.class);
    public static final int TPL_NOTEMAIL = 106;
    private SendData sendData = null;

    private SendSms sendSms = new SendSms();

    public SmsSend(SendData sendData) {
        this.sendData = sendData;
    }

    public static int getCounts(String entityLists, String entityTypeList) {
        Set<String> count = new HashSet<String>();
        String[] entityList = entityLists.split(",");
        int i = 0;
        for (String entity : entityTypeList.split(",")) {
            if (entity.equals("job")) {
                count.add(CommonUtil.getCidByJid(entityList[i]));
            } else if (entity.equals("project")) {
                count.add(CommonUtil.getCidByPid(Integer.valueOf(entityList[i])));
            } else {
                count.add(CommonUtil.getCidBySid(Integer.valueOf(entityList[i])));
            }
            i++;
        }
        return count.size();

    }

    public static int getLocation(String getEntityTypeList, String type) {
        int i = 0;
        for (String entity : getEntityTypeList.split(",")) {
            if (type.equals(entity)) {
                return i;
            }
            i++;
        }
        return 0;
    }

    public void testSend() {
        logger.info("smsSendTest start");

        List<Integer> testMobiles = new ArrayList<Integer>();
       // testMobiles.add(880225);
        testMobiles.add(154902);
        testMobiles.add(34105731);

        for (int uid : testMobiles) {
            try {
                MessageData messageData = null;
                while (!sendData.isEmpty()) {
                    messageData = sendData.popSendData();
                    String entityTypeFirst = messageData.getEntityTypeList().split(",")[0];
                    String entityFirst = messageData.getEntityList().split(",")[0];
                    String toMobile = CommonUtil.getMobileByUid(uid);
                    String smsUrl = "";
                    String corpName = "";
                    if (entityTypeFirst.equals("job")) {
                        logger.info("test --> job:{} uid:{} mobile:{}", entityFirst, messageData.getUid(), toMobile);
                        corpName = CommonUtil.getCorpNameByJid(entityFirst);
                        String loggerNeed = "ch=SendInviteSmsNotEmail" + TPL_NOTEMAIL + "&type=job" + "&uid=" + messageData.getUid();
                        smsUrl = CommonUtil.smsUrlForJob(loggerNeed);
                    } else if (entityTypeFirst.equals("project")) {
                        logger.info("test --> project:{} uid:{} mobile:{}", entityFirst, messageData.getUid(), toMobile);
                        corpName = CommonUtil.getCorpNameByPid(Integer.valueOf(entityFirst));
                        String loggerNeed = "ch=SendInviteSmsNotEmail" + TPL_NOTEMAIL + "&type=project" + "&uid=" + messageData.getUid();
                        smsUrl = CommonUtil.smsUrlForProject(loggerNeed);
                    } else if (entityTypeFirst.equals("campus")) {
                        logger.info("test --> campus:{} uid:{} mobile:{}", entityFirst, messageData.getUid(), toMobile);
                        corpName = CommonUtil.getCorpNameBySid(Integer.valueOf(entityFirst));
                        String loggerNeed = "ch=SendInviteSmsNotEmail" + TPL_NOTEMAIL + "&type=campus" + "&uid=" + messageData.getUid();
                        smsUrl = CommonUtil.smsUrlForSchedule(loggerNeed);
                    }
                    Map<String, String> map = new HashMap<String, String>();
                    map.clear();
                    map.put("uName", CommonUtil.getUnameByUid(messageData.getUid()));
                    map.put("counts", getCounts(messageData.getEntityList(), messageData.getEntityTypeList()) + "");
                    map.put("corpName", corpName);
                    map.put("MOBILE", toMobile);
                    map.put("UID", messageData.getUid() + "");
                    map.put("smsUrl", smsUrl);
                    // sendSms.sendSmsBatchNew(toMobile, String.valueOf(TPL_NOTEMAIL), map);
                    String result = sendSms.sendSmsSingle(toMobile, Integer.toString(TPL_NOTEMAIL), map);
                    //  String result = "[\\\"2342\\\"]";
                    if (result.contains("1")) {
                        logger.info("test --> sendSms success map:{}", map.toString());
                        break;
                    } else {
                        continue;
                    }
                }
            } catch (Exception e) {
                logger.error("test -->  error message:" + e.getMessage(), e);
            }
        }
        logger.info("smsSendTest end");


    }

    @Override
    public void run() {
        while (!sendData.isEmpty()) {
            MessageData messageData = sendData.popSendData();
            if (messageData == null)
                continue;
            Map<String, String> map = new HashMap<String, String>();
            try {
                if (!CommonUtil.isHaveEmail(messageData.getUid()) && !StringUtil.isEmpty(CommonUtil.getMobileByUid(messageData.getUid()))) {
                    int counts = 0;
                    String toMobile = "";
                    String corpName = "";
                    String uName = "";
                    String smsUrl = "";
                    String entityTypeFirst = null;
                    String entityFirst = null;
                    //start 修改  项目-->宣讲会-->职位
                    String[] entitySum = messageData.getEntityList().split(",");
                    if (messageData.getEntityTypeList().contains("project")) {
                        entityTypeFirst = "project";
                        entityFirst = entitySum[getLocation(messageData.getEntityTypeList(), entityTypeFirst)];
                    } else if (messageData.getEntityTypeList().contains("campus")) {
                        entityTypeFirst = "campus";
                        entityFirst = entitySum[getLocation(messageData.getEntityTypeList(), entityTypeFirst)];
                    } else if (messageData.getEntityTypeList().contains("job")) {
                        entityTypeFirst = "job";
                        entityFirst = entitySum[getLocation(messageData.getEntityTypeList(), entityTypeFirst)];
                    } else {
                        logger.info("main --> get first");
                        entityTypeFirst = messageData.getEntityTypeList().split(",")[0];
                        entityFirst = messageData.getEntityList().split(",")[0];
                    }
                    //end
                    toMobile = CommonUtil.getMobileByUid(messageData.getUid());
                    counts = getCounts(messageData.getEntityList(), messageData.getEntityTypeList());
                    uName = CommonUtil.getUnameByUid(messageData.getUid());
                    if (entityTypeFirst.equals("job")) {
                        logger.info("main --> job:{} uid:{} mobile:{}", entityFirst, messageData.getUid(), toMobile);
                        corpName = CommonUtil.getCorpNameByJid(entityFirst);
                        String loggerNeed = "ch=SendInviteSmsNotEmail" + TPL_NOTEMAIL + "&type=job" + "&uid=" + messageData.getUid();
                        smsUrl = CommonUtil.smsUrlForJob(loggerNeed);
                    } else if (entityTypeFirst.equals("project")) {
                        logger.info("main --> project:{} uid:{} mobile:{}", entityFirst, messageData.getUid(), toMobile);
                        corpName = CommonUtil.getCorpNameByPid(Integer.valueOf(entityFirst));
                        String loggerNeed = "ch=SendInviteSmsNotEmail" + TPL_NOTEMAIL + "&type=project" + "&uid=" + messageData.getUid();
                        smsUrl = CommonUtil.smsUrlForProject(loggerNeed);
                    } else if (entityTypeFirst.equals("campus")) {
                        logger.info("main --> campus:{} uid:{} mobile:{}", entityFirst, messageData.getUid(), toMobile);
                        corpName = CommonUtil.getCorpNameBySid(Integer.valueOf(entityFirst));
                        String loggerNeed = "ch=SendInviteSmsNotEmail" + TPL_NOTEMAIL + "&type=campus" + "&uid=" + messageData.getUid();
                        smsUrl = CommonUtil.smsUrlForSchedule(loggerNeed);
                    }
                    map.clear();
                    map.put("uName", uName);
                    map.put("counts", counts + "");
                    map.put("corpName", corpName);
                    map.put("MOBILE", toMobile);
                    map.put("UID", messageData.getUid() + "");
                    if (!StringUtil.isEmpty(uName) && !StringUtil.isEmpty(counts + "") && !StringUtil.isEmpty(corpName) && !StringUtil.isEmpty(toMobile)) {
                        map.put("smsUrl", smsUrl);
                        //  String fileData = messageData.getUid() + "\t" + uName + "\t" + corpName + "\t" + counts + "\t" + smsUrl + "\t" + toMobile;
                        // WriteFile(fileData);
                        sendSms.sendSmsBatchNew(toMobile, String.valueOf(TPL_NOTEMAIL), map);
                    } else {
                        logger.info("main --> getInfo lost UID:{} uName:{} counts:{} corpName:{} toMobile:{} tplId:{} Type:{} map:{}", messageData.getUid(), uName, counts, corpName, toMobile, TPL_NOTEMAIL, entityTypeFirst, map.toString());
                    }
                }
            } catch (Exception e) {
                logger.error("main --> error UID:{} ", messageData.getUid());
                logger.error("main -->  error message:" + e.getMessage(), e);
            }
        }
        if (sendSms.getLeftSms().size() > 0 && sendSms.getLeftSms().size() < 50) {
            sendSms.sendSmsBatch(sendSms.getLeftSms());
        }
    }


}
